package Dao.Imp;

import Dao.ValidateDao;
import modal.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by tan on 2017/8/22.
 */
@Repository
@Transactional
public class ValidateDaoImp implements ValidateDao {

    @Autowired
    SessionFactory sessionFactory;

    public String validateUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        try {
            String name =user.getUsername();
            String password = user.getPassword();
            System.err.println("name: "+name+", password: "+password);
            StringBuilder Sql  = new StringBuilder();
            Sql.append("select * from person_info where username = ? AND PASSWORD =?");
            Query query=session.createSQLQuery(Sql.toString()).setString(0,name).setString(1,password);
            if(query.uniqueResult()!=null)
            {
                return "success";
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
}
